package edu.hubu.factory;

import edu.hubu.exception.UnsupportedShapeException;
import edu.hubu.shape.IShape;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

/**
 * @author moonlan
 * date 2021/3/30 上午9:08
 */
@SuppressWarnings(value = "unused")
public class ShapeFactory {

    private final Properties properties;

    private ShapeFactory(String properties) {
        this.properties = new Properties();
        try {
            this.properties.load(getClass().getClassLoader().getResourceAsStream(properties));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static ShapeFactory getFactoryInstance(String properties) {
        return new ShapeFactory(properties);
    }

    public IShape getShapeInstance() {
        String property = this.properties.getProperty("shape.className");
        try {
            Class<?> aClass = Class.forName(property);
            Object instance = aClass.getConstructor().newInstance();
            return (IShape) instance;
        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            throw new UnsupportedShapeException(e);
        }
    }

}
